package com.rong.action;
 
 import java.util.List;
import java.util.Map;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;
 import com.rong.DAO.ZynUserDAO;
 import com.rong.ORM.ZynCar;
import com.rong.ORM.ZynUser;
import com.rong.hibernate.Const;

public class RegisterAction extends ActionSupport {

    private ZynUserDAO userDao = new ZynUserDAO();

    private ZynUser user;    //注册提交的用户对    
    private String repwd;    //重复密码
  
    private List<ZynUser> list;
	private String saveFlag="";
	private int id=-1; // the car id indatabase.

    public ZynUser getUser() {
        return user;
    }

    public void setUser(ZynUser user) {
        this.user = user;
    }

    public String getRepwd() {
       return repwd;
    }

    public void setRepwd(String repwd) {
        this.repwd = repwd;
    }

    /**
	 * @return the list
	 */
	public List<ZynUser> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ZynUser> list) {
		this.list = list;
	}

	/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}

	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	@Override
    public String execute() throws Exception {
        //User user = new User();
		if(userDao.findByName(this.user.getName()).size()==0){
        userDao.save(this.user);        //保存注册的user对象
        this.setSaveFlag("保存成功,请重新登录然后再进行授权操作.");
		}else{
		this.setSaveFlag("帐号重复,请使用其他帐号..");	
			
		}
        return SUCCESS;
    }
    public String findall() throws Exception {
        
    	
    	 ActionContext actionContext = ActionContext.getContext();         
         Map session = actionContext.getSession(); 
         ZynUser loguser=(ZynUser) session.get("loginUser");    // the user session
         System.out.println("in registerAction: loguser="+loguser.getName());
         if(loguser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有
        	 this.setList(userDao.findAll());        //所有用户
        	 System.out.println("总部");
         }else{
        	 this.setList(userDao.findByComId(loguser.getComId()));        //用户所在公司的所有用户
        	 System.out.println("分公司或其他 comprty="+loguser.getZyncom().getComName());
         }
    	
		
	        return "success_list";
	    }
	 public String findbyid() throws Exception {
	        
		this.setUser(userDao.findById(this.getId()));// find the car by id			
	        return "s_find_one";
	       
	    }
	 public String edit() throws Exception {
	      			
		 userDao.merge(this.getUser());// edit the car by id			
		    return "s_edit_one";
		    }
	 public String delete() throws Exception {
	        
			userDao.delete(userDao.findById(this.getId()));// delete the car by id			
		    return "s_delete_one";
		    }
	
	 
    @Override
    public void validate() {
       //验证码验证
//        if(null == inputCertCode || "".equals(inputCertCode)){
//            this.addFieldError("inputCertCode", "验证码不能为空!");
//        }else{
//            Map session = ActionContext.getContext().getSession();
//            String certCode = session.get("certCode").toString();
//            if( ! certCode.equals(this.inputCertCode)){
//                this.addFieldError("inputCertCode", "验证码不正确!");
//            }
 //       }
        
    }
        
}
